package org.gordonframework.site.transitions {
	import org.gordonframework.core.events.GProcessEvent;
	import org.gordonframework.core.tween.GTween;
	import org.gordonframework.site.transitions.GTransition;
	import org.gordonframework.site.views.GView;	

	/**
	 * @author sampet
	 */
	public class GOverlayFade extends GTransition {

		private var inDuration:Number;
		private var outDuration:Number;

		private var tweeningOut:Boolean;

		private var overlay:GView;

		public function GOverlayFade(overlay:GView, inDuration:Number = 0.5, outDuration:Number = 0.5) {
			this.overlay = overlay;
			this.outDuration = outDuration;
			this.inDuration = inDuration;
			addChild( overlay );
		}

		override protected function hideView():void {			
			if(!currentView) {
				hideViewComplete();
			}
			else if(!tweeningOut) {
				tweeningOut = true;
				overlay.visible = true;
				var tween:GTween = new GTween( overlay, {alpha:1}, outDuration * durationMultiplier, currentView.outro(durationMultiplier) );
				tween.addEventListener( GProcessEvent.COMPLETE, tweenOutCompleteHandler );
				tween.start( );
			}
		}

		private function tweenOutCompleteHandler(event:GProcessEvent):void {
			tweeningOut = false;
			currentView.visible = false;
			hideViewComplete();	
		}

		override protected function showView():void {
			currentView.visible = true;
			var tween:GTween = new GTween( overlay, {alpha:0}, inDuration * durationMultiplier );
			tween.addEventListener( GProcessEvent.COMPLETE, tweenInCompleteHandler );
			tween.start( );
		}

		private function tweenInCompleteHandler(event:GProcessEvent):void {
			overlay.visible = false;
			currentView.intro( durationMultiplier );
			showViewComplete();
		}		
	}
}
